package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;

public class ForgetPasswordActivity extends BaseActivity {
    private EditText ed_password_one;
    private EditText ed_password_two;

    private ImageView img_show_one;
    private ImageView img_show_two;
    private TextView tv_ok;
    private LinearLayout ll_fanhui;

    private boolean oneShow = false;
    private boolean twoShow = false;

    //忘记密码
    @Override
    public int getContentViewId() {
        return R.layout.activity_forgetpassword;
    }

    @Override
    public void findView() {
        setNoTitle();
        ed_password_one = findViewById(R.id.ed_password_one);
        ed_password_two = findViewById(R.id.ed_password_two);
        img_show_one = findViewById(R.id.img_show_one);
        img_show_two = findViewById(R.id.img_show_two);
        tv_ok = findViewById(R.id.tv_ok);
        ll_fanhui = findViewById(R.id.ll_fanhui);
    }

    @Override
    public void setListener() {
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_show_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (oneShow) {
                    oneShow = false;
                    img_show_one.setImageResource(R.mipmap.mima_false);
                    ed_password_one.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    oneShow = true;
                    img_show_one.setImageResource(R.mipmap.mima_true);
                    ed_password_one.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
        img_show_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (twoShow) {
                    twoShow = false;
                    img_show_two.setImageResource(R.mipmap.mima_false);
                    ed_password_two.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    twoShow = true;
                    img_show_two.setImageResource(R.mipmap.mima_true);
                    ed_password_two.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
    }

    @Override
    public void afterCreate() {
        String nextCode = getIntent().getStringExtra("nextCode");
        String phone = getIntent().getStringExtra("phone");


        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forgetPassword(nextCode, phone);
            }
        });
    }

    public void forgetPassword(String nextCode, String phone) {
        String password_one = ed_password_one.getText().toString();
        String password_two = ed_password_two.getText().toString();
        if (StUtils.isEmpty(password_one)) {
            showToast("请输入密码");
            return;
        }

        if (StUtils.isEmpty(password_two)) {
            showToast("请输入密码");
            return;
        }
        if (!password_one.equals(password_two)) {
            showToast("两次输入密码不一致请修改");
            return;
        }
        if (!StUtils.isPasswordForm(password_one)) {
            showToast("密码须有字母和数字8位以上");
            return;
        }
        if (!StUtils.isPasswordForm(password_two)) {
            showToast("密码须有字母和数字8位以上");
            return;
        }
        post(Url.resetPassword, "", String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                if (response.contains("200")) {
                    Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public boolean onFail(String response) {
                return false;
            }
        }, "{\"mobile\":\"" + phone + "\",\"nextCode\":\"" + nextCode + "\",\"password\":\"" + password_one + "\"}");
    }


}
